package com.qf.mybatis01.entity;

import cn.hutool.core.util.RandomUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book {
    private Integer id;
    private String name;
    private String author;
    private String publish;
    private Integer sort;
    //随机生成一本书静态方法
    public static Book randomBook(){
        String name= RandomUtil.randomString(10);
        String author= RandomUtil.randomString(10);
        String publish= RandomUtil.randomString(20);
        Integer sort= RandomUtil.randomInt(1,500000);
        return new Book(null,name,author,publish,sort);
    }
    //随机生成bookList
    public static List<Book> randomBookList(Integer size){
        ArrayList<Book> books = new ArrayList<>();

        for (int i=0;i<size;i++){
            books.add(randomBook());
        }

        return books;
    }

    /*
    * list<Book>---->list<String>
    String格式为csv
    */
    public static List<String> randomBookCSVList(Integer size){
        ArrayList<String> books = new ArrayList<>();

        for (int i=0;i<size;i++){
            books.add(randomBook().toCSVString());
        }
        return books;
    }

    public static Book cvsToBook(String s) {
        String[] split = s.split(",");
        String name=split[0];
        String author=split[1];
        String publish=split[2];
        Integer sort=Integer.parseInt(split[3]);
        return new Book(null,name,author,publish,sort);
    }

    public String toCSVString(){
        String s=this.getName()+","+this.getAuthor()+","+this.getPublish()+","+this.getSort();
        return s;
    }

    public static void main(String[] args) {
        System.out.println(Book.randomBook());
        System.out.println(Book.randomBookList(10));
    }
}
